package com.example.Android_test.util;

import android.content.Context;
import android.util.Log;
import com.example.Android_test.activity.Main;

import java.io.*;

/**
 * Created with IntelliJ IDEA
 * User: anpi0413
 * Date: 17.05.13
 * Time: 18:13
 * To change this template use File | Settings | File Templates.
 */
public class FileUtils {
    private static Context context = Main.getAppContext();
    private static boolean debug = true;

    public static String loadFileToString(String fileName) throws Exception{
        InputStream inputStream = context.openFileInput(fileName);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String result = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            result += tmp;
        }
        inputStream.close();

        if (debug) Log.d(Main.LOG_TAG, ".loadFileToString(" + fileName + ") OK");

        return result;
    }

    public static boolean isInternalFileExist(String fileName) {
        File file;
        try {
            file = new File(context.getFilesDir() + "/" + fileName);
            if (debug) Log.d(Main.LOG_TAG, ".isInternalFileExist(" + fileName + ") " + file.exists());
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, "isInternalFileExist " + fileName + ". " + e.getMessage());
            return false;
        }

        return file.exists();
    }

    public static void saveResourcesToInternalStorage(String fileName, int idOfRes) {
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);

            inputStream = context.getResources().openRawResource(idOfRes);

            int nRead;
            byte[] data = new byte[1024];

            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                fileOutputStream.write(data, 0, nRead);
            }

            fileOutputStream.flush();

            if (debug) Log.d(Main.LOG_TAG, ".saveResourcesToInternalStorage(" + fileName + ") OK");
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, "saveResourcesToInternalStorage " + fileName + ". " + e.getMessage());
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
                if (inputStream != null) inputStream.close();
            } catch (Exception e) {
                Log.e(Main.LOG_TAG, "saveResourcesToInternalStorage " + fileName + ". " + e.getMessage());  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    public static void saveStringToInternalStorage(String fileName, String xmlContentSTring) {
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);

            inputStream = new ByteArrayInputStream(xmlContentSTring.getBytes("UTF-8"));

            int nRead;
            byte[] data = new byte[1024];

            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                fileOutputStream.write(data, 0, nRead);
            }

            fileOutputStream.flush();

            if (debug) Log.d(Main.LOG_TAG, ".saveResourcesToInternalStorage(" + fileName + ") OK");
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, "saveResourcesToInternalStorage " + fileName + ". " + e.getMessage());
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
                if (inputStream != null) inputStream.close();
            } catch (Exception e) {
                Log.e(Main.LOG_TAG, "saveResourcesToInternalStorage " + fileName + ". " + e.getMessage());  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

}
